/**
 * Created by Mark on 2018/4/19.
 * 订单详情
 */

require("./order-detail.css");
require("page/common/nav/nav.js");
require("page/common/header/header.js");
var navSide = require("page/common/nav-side/nav-side.js");
var _order = require("service/order-service.js");
var _mm = require("util/mm.js");
var orderDetailTemplate = require("./order-detail.string");

var page = {
    data : {
        orderNumber : _mm.getUrlParam('orderNumber')
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },

    onLoad : function(){
        this.loadNavSide();//加载侧边导航
        this.loadOrderDetail();//加载订单详情
    },
    bindEvent : function () {
        this.clickCancelOrderBtn();//点击取消订单
        this.clickOrderPayBtn();//点击支付
        this.clickGetGoodsBtn();//确认收货
    },
    //1.页面初始化函数
    loadNavSide : function () {//1.1加载侧边导航
        navSide.init({
            name : "order-list",
        });
    },
    loadOrderDetail : function () {//1.2加载订单详情
        var _this = this,
            orderDetailHTML = "",
            $content  = $(".content");
        _order.getOrderDetail(_this.data.orderNumber,
            function (res) {
                _this.dataFilter(res.data);
                orderDetailHTML = _mm.renderHtml(orderDetailTemplate,res.data);
                $content.html(orderDetailHTML);
            }, function (err) {
                _mm.errorTips(err.msg)
            }
        );
    },

    //2.事件绑定函数
    clickCancelOrderBtn : function () {//2.1点击取消订单
        var _this = this;
        $(document).on("click",".order-cancel", function () {
            if(window.confirm("确定要取消该订单吗？")){
                _order.cancelOrder(_this.data.orderNumber,
                    function () {
                        _mm.successTips("订单取消成功！")
                        _this.loadOrderDetail();
                }, function (err) {
                        _mm.errorTips(err.msg);
                    });
            }
        });
    },
    clickGetGoodsBtn : function () {//2.1确认收货
        var _this = this;
        $(document).on("click",".order-getGoods", function () {
            if(window.confirm("确定已经收货了吗？")){
                _order.getGoods(_this.data.orderNumber,
                    function (res) {
                        _mm.successTips(res.data)
                        _this.loadOrderDetail();
                    }, function (err) {
                        _mm.errorTips(err.msg);
                    });
            }
        });
    },
    clickOrderPayBtn : function () {//2.2点击支付
        var _this = this;
        $(document).on("click",".order-pay", function () {
            window.location.href = "./payment.html?orderNumber="+_this.data.orderNumber
        });
    },

    //【】
    //3.辅助函数
    dataFilter : function (data) { // 数据的适配
        data.needPay = data.status == 10;
        data.isSended = data.status == 40; //已经发货
        data.isClosed = data.status == 50; //已经收货
    }
};

$(function(){
    page.init();
});

